
const express = require('express');
const router = express.Router()
// 导入数据库 sqlFn('sql',[],res=>{})
const sqlFn  = require('./mysql')



const vipLogin = require('./login/vip_login.json');
const adminLogin = require('./login/admin_login.json');

const adminPermission = require('./login/admin_permission.json');
const vipPermission = require('./login/vip_permission.json');


// 路由 接口地址
// 导入 mockjs
const Mock = require('mockjs')

const data = require('./data/format.json');


// 登录 -  分权限 1 超级管理员 admin  2 其他账号 vip 账号 
router.post('/login',(req,res)=>{
   const username = req.body.user;
   const pwd = req.body.pwd;
   if(username === 'admin'){
      res.send(adminLogin)   // 超级管理
   }else{
      res.send(vipLogin)     // VIP 用户
   }
})


// 用户权限数据接口
router.get('/permission',(req,res)=>{
   const token = req.query.token;
   console.log('99999',req.query);
   if(token === 'admin'){
      res.send(adminPermission)
   }else{
      res.send(vipPermission)
   }
})


// 首页 销量额数据统计 
router.get('/home/dataCount',(req,res)=>{
    res.send(Mock.mock({
       info:'数据统计',
       success:true,
       data:{
          "list|4":[{
             "id|+100":1,
             "viewsTotal|10000-100000":1,
             "views|1-30000":1,
             "saleTotal|1000-100000":1,
             "sale|1-100000":1,
             "payTotal|1000-999999":1,
             "pay|1-9999":1,
             "collectTotal|99-99999":1,
             "collect|0-9999":1
          }]
       }
    }))
})

// 首页 折线图 数据统计 月销量 月销售
router.get('/home/format',(req,res)=>{
   res.send(data)
})

// 首页 -- 今日 -- 订单统计
router.get('/home/orderInfo',(req,res)=>{
   res.send(Mock.mock({
       info:'订单统计信息',
       success:true,
       "list":
         {
          "orderCount|1-100000":1,
          "curOrderCount|1-100000":1,
          'count':function(){
               if(this.curOrderCount > this.orderCount){
                 [this.orderCount,this.curOrderCount] = [this.curOrderCount,this.orderCount]
               }
          },
          "money|1-200000":1,
          "curMoney|1-1000":1,
          "moneyfun":function(){
             if(this.curMoney > this.money){
                [this.money,this.curMoney] = [this.curMoney,this.money]
             }
          },
          "collect|1-100000":1,
          "curCollect|1-100000":1,
          "department|1-100000":1,
          "branchSchool|1-100000":1
         }
   }))
})



// 产品列表 
router.get('/home/goodsList',(req,res)=>{
    res.send(Mock.mock({
       info:'数据统计',
       success:true,
       data:{
          "list|4":[{
             "barcode":'',
             "category":"家具/家居",
             "cid":1004,
             "created":new Date(),
             "descs":"<p>tttt123</p>",
             "id":10140,
             "num":568,
             "image":'<img src="https://lmg.jj20.com/up/allimg/1114/040221103339/210402103339-8-1200.jpg" />',
             "paramsInfo":"[{\"value\":\"纯棉66\",\"title\":\"材料\",\"children\":[{\"value\":\"新疆棉花66\",\"title\":\"纯棉\"},{\"value\":\"嗯嗯66\",\"嗯嗯666\",\"title\":\"类似\"}]}]",
             "price":"888",
             "sellPoint":"8888",
             "status":"1",
             "title":"8888",
             "updated":""
          }]
       },
       pageSize:5,
       status:200,
       total:32
    }))
})


// 产品列表 
//router.get('/home/goodsList',(req,res)=>{
//  const page = req.query.page || 1;
   // const sqlLen = "select * from project where id";
   // sqlFn(sqlLen,null,data=>{
   //   let len = data.length;
   //   const sql = "select * from project order by id desc limit 8 offset" + (page-1) * 8;
   //   sqlFn(sql,null,result=>{
   //      if(result.length > 0){
   //         res.send({
   //             status:200,
   //             data:result,
   //             pageSize:8,
   //             total:len,
   //         })
   //      }else{
   //         res.send({
   //            status:200,
   //            msg:'暂无数据'
   //         })
   //      }
   //   })
   // })
//})



// 产品查询
//router.get('/home/goodsSearch',(req,res)=>{
   //    var search = req.query.search;
   // const sql = "select * from project where concat(`title`,`sellPoint`,`descs`) like '%" + search + "%";
   // sqlFn(sql,null,result=>{
      //      if(result.length > 0){
      //      res.send({
      //          status:200,
      //          result,
      //      })
      //   }else{
         //      res.send({
         //      status:500,
         //      msg:'暂无数据'
         //   })
         //}
         //})
//})


// 产品查询
router.get('/home/goodsSearch',(req,res)=>{
   res.send(Mock.mock({
      info:'数据统计',
      success:true,
      data:{
         "list|4":[{
            "id":10050,
            "image":"",
            "paramsInfo":"[{\"value\":\"纯棉66\",\"title\":\"材料\",\"children\":[{\"value\":\"新疆棉花66\",\"title\":\"纯棉\"},{\"value\":\"嗯嗯66\",\"嗯嗯666\",\"title\":\"类似\"}]}]",
            "price":"888",
            "sellPoint":"8888",
            "status":"1",
            "title":"888",
         }]
      },
      pageSize:5,
      status:200,
      total:32
   }))
})


// 删除商品
router.get('/home/deleteItemById',(req,res)=>{
    var id = req.query.id;
    const sql = "delete from project where id=?"
    const arr = [id];
    sqlFn(sql,arr,result=>{
       if(result.affectedRows > 0){
          res.send({
             status:200,
             msg:'删除成功'
          })
       }else{
         res.send({
            status:500,
            msg:'删除失办'
         })
       }
    })
})




// 
router.get('/home/goodsItemCategory',(req,res)=>{
   res.send(Mock.mock({
      data:{
         "result":[
            {
              "id":1,
              "name":"水果",
              "cid":1001,
              "type":1
           },
           {   
              "id":2,
              "name":"蔬菜",
              "cid":1002,
              "type":1
           },
           {
               "id":3,
               "name":"肉类",
               "cid":1003,
               "type":1
           },
         ]
      },
      status:200,
   }))
})



// 上传图片
//var storage = multer.diskStorage({
   //  destination(req,file,cd){
      // cd(null,'./upload/')
      //},
      //filename(req,file,cb){
         //cb(null,Date.now() + '-' + file.originalname)
         //}
         //})
         //var createFolder = (folder) =>{
            //try{
               //  fs.accessSync(folder);
               //}catch(e){
                  // fs.mkdirSync(folder);
                  //}
                  //} 

//var uploadFolder = './upload/';
//createFolder(uploadFolder);
//var upload = multer({
  // storage:storage
//}); 

// 上传图片
//router.post('/upload/',upload.single('file'),function(req,res,next){
//   var file = req.file;
 //  console.log('11',file.minetype);
 //  console.log('22',file.originalname);
 //  console.log('33',file.size);
 //  console.log('44',file.path);
 //  res.json({
 //     res_code:'0',
 //     name:file.originalname,
 //     url:file.path
 //  })
//})

// 富文本服务器
//router.post('/batchUpload',upload.single('file'),function(req,res,next){
//   var file = req.file;
//   console.log('11',file.minetype);
 //  console.log('22',file.originalname);
 //  console.log('33',file.size);
 //  console.log('44',file.path);
 //  res.json({
 //     res_code:'0',
 //     name:file.originalname,
 //     url:file.path
 //  })
//})


// 批量删除
router.get('/goods/batchDelte',(req,res)=>{
    let ids = req.query.ids;
    const sql = `delete from project where id in (${ids})`;
    sqlFn(sql,null,result=>{
       if(result.affectedRows > 0){
           res.send({
              status:200,
              msg:'删除成功'
           })
       }else{
           res.send({
              status:500,
              msg:'删除失办'
           })
       }
    })
})



// 所有类目管理 增加一级导航
/*
router.get('/itemCategory/insertCategory',(req,res)=>{
   const name = req.query.name;
   const cid = Math.ceil(new Date().getTime()/1000);
   const sql = 'insert into category values (null,?,?,'1')'
   sqlFn(sql,[name,cid],result=>{
      if(result.affectedRows > 0){
          res.send({
              status:200,
              msg:'添加成功'
          })
      }else{
         res.send({
            status:500,
            msg:'添加失败'
        })
      }
   })
})  */



// 所有类目管理 增加子导航
/* router.get('/itemCategory/insertItemCategory',(req,res)=>{
   const type = req.query.cid;
   const name = req.query.name;
   const cid = Math.ceil(new Date().getTime()/1000);
   const sql = 'insert into category values (null,?,?,?)'
   sqlFn(sql,[name,cid,type],result=>{
      if(result.affectedRows > 0){
          res.send({
              status:200,
              msg:'添加成功'
          })
      }else{
         res.send({
            status:500,
            msg:'添加失败'
        })
      }
   })
}) */




// 分类管理  删除导航
/* router.get('/content/deleteContentCategoryById',(req,res)=>{
   const id = req.query.id;
   const sql = 'delete from category where id=?'
   sqlFn(sql,[id],result=>{
      if(result.affectedRows > 0){
          res.send({
              status:200,
              msg:'删除成功'
          })
      }else{
         res.send({
            status:500,
            msg:'删除失败'
        })
      }
   })
}) */


// 分类管理  修改导航
/* router.get('/itemCategory/updateCategory',(req,res)=>{
   const id = req.query.id;
   const name = req.query.name;
   const sql = 'update category set name=? where id=?'
   sqlFn(sql,[name,id],result=>{
      if(result.affectedRows > 0){
          res.send({
              status:200,
              msg:'修改成功'
          })
      }else{
         res.send({
            status:500,
            msg:'修改失败'
        })
      }
   })
}) */






module.exports = router;